/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {TreeNode}
 */
var sortedListToBST = function(head) {
    return fun(head, null)
};
// >=s, <e
function fun(s, e){
    let fast = s,
        slow = s;
    if(s === e)return null;
    while(fast!==e&&fast.next!==e){
        fast = fast.next.next;
        slow = slow.next;
    }
    let node = new TreeNode(slow.val);
    node.left = fun(s, slow);
    node.right = fun(slow.next, e);

    return node;
}